﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TransactionServer.OSActivities77;
using TransactionServer.General_Classes;


namespace TransactionServer.Connectors.Test
{
    [TestClass]
    public class OSConnector77Tests
    {
        OSConnector77 connector;

        [TestMethod]
        public void TestTableParts()
        {
            var document = new Entity("1C", "Документ.Тест");
            document["НомерДок"] = new Random().Next(100000);
            document["ДатаДок"] = DateTime.Now.ToString("s");
            document["ТестСтрока"] = "1123";

            var comDoc = connector.ConvertFrom(document);
            document.Id = connector.SaveEntity(comDoc);

            var document1 = new Entity("1C", "ТабличнаяЧасть.Тест");
            document1["ТестСтрокаТабл"] = "StringTest";
            document1["ТестЧислоТабл"] = "1.0";
            document1["Документ"] = document.Id;

            var comDoc1 = connector.ConvertFrom(document1);
            document1.Id = connector.SaveEntity(comDoc1);

            var document2 = new Entity("1C", "ТабличнаяЧасть.Тест");
            document2["ТестСтрокаТабл"] = "StringTest1";
            document2["ТестЧислоТабл"] = "2.0";
            document2["Документ"] = document.Id;

            var comDoc2 = connector.ConvertFrom(document2);
            document2.Id = connector.SaveEntity(comDoc2);
        }

        [TestMethod]
        public void TestGetNextEvent()
        {
            var evnt = connector.getNextEvent();
            if (evnt != null)
            {
                Assert.IsTrue(!String.IsNullOrEmpty(evnt.Id));
                Assert.IsTrue(!String.IsNullOrEmpty(evnt.MessageName));
                Assert.IsNotNull(evnt.Entity);
                Assert.IsTrue(!String.IsNullOrEmpty(evnt.Entity.Id));
            }
        }

        [TestMethod]
        public void TestGetEntities()
        {
            var entities = connector.getEntities();
            Assert.IsTrue(entities.Count > 0);
        }

        [TestMethod]
        public void TestGetAttributes()
        {
            var entities = connector.getEntities();
            foreach (var entity in entities)
            {
                var attributes = connector.getAttributes(entity);
                Assert.IsTrue(attributes.Count > 0);
            }
        }

        [TestMethod]
        public void TestConvertFrom()
        {
            var ent = new Entity("1C", "Справочник.Тест");
            ent["Наименовение"] = "Имя";

            var comObject = connector.ConvertFrom(ent);
        }

        [TestMethod]
        public void TestlookupListByValue()
        {
            object obj = connector.lookupListByValue("Тест", "Тест2");
            Assert.AreEqual("Тест2", connector.InvokeObjectMethod(obj, "Идентификатор"));
        }

        [TestMethod]
        public void TestgetListName()
        {
            string obj = connector.getListName("{\"E\",\"0\",\"0\",\"20\",\"0\",\"0\",\"        21   \"}");
            Assert.AreEqual("Тест 1", obj);
        }

        [TestMethod]
        public void TestlookupFieldById()
        {
            var entity = new Entity("1C", "Справочник.Тест", "{\"B\",\"0\",\"0\",\"12\",\"0\",\"0\",\"         1   \"}");
            connector.lookupFieldsById(entity, new string[] { "Код", "Наименование", "ТестСтрока" });

            Assert.AreEqual("1", entity["Код"]);
            Assert.AreEqual("123", entity["Наименование"]);
            Assert.AreEqual("456", entity["ТестСтрока"].ToString().Trim());

            var name = connector.lookupFieldById(entity.Id, "Наименование");

            Assert.AreEqual("123", name);
        }

        [TestMethod]
        public void TestlookupDictionaryByField()
        {
            var id = connector.lookupDictionaryByField("Справочник.Тест", "Наименование", "123");
            Assert.AreEqual("{\"B\",\"0\",\"0\",\"12\",\"0\",\"0\",\"         1   \"}", id);

            var id1 = connector.lookupDictionaryByField("Справочник.Тест", "Код", "1");
            Assert.AreEqual("{\"B\",\"0\",\"0\",\"12\",\"0\",\"0\",\"         1   \"}", id1);

            var id2 = connector.lookupDictionaryByField("Справочник.Тест", "ТестСтрока", "456");
            Assert.AreEqual("{\"B\",\"0\",\"0\",\"12\",\"0\",\"0\",\"         1   \"}", id2);
        }

        [TestMethod]
        public void TestgetObjectList()
        {
            var table = connector.getObjectList("Справочник.Тест");

            Assert.IsTrue(table.Rows.Count > 0);
        }

        [TestMethod]
        public void TestDelete()
        {
            var entity = new Entity("1С", "Справочник.Тест");
            string code = (new Random().Next(100000) + 1000).ToString();

            entity["Наименование"] = "Hello World!";
            entity["Код"] = code;

            object comObject = connector.ConvertFrom(entity);
            entity.Id = connector.SaveEntity(comObject);

            connector.Delete(entity);

        }

        [TestMethod]
        public void TestMoveDocument()
        {
            var entity = new Entity("1C", "Документ.Тест");
            entity["НомерДок"] = new Random().Next(200000).ToString();
            entity["ТестСтрока"] = "ТестСтрока";

            var comObj = connector.ConvertFrom(entity);
            entity.Id = connector.SaveEntity(comObj);

            connector.MoveDocument(comObj);

            var moved = connector.lookupFieldById(entity.Id, "Проведен");

            Assert.AreEqual("true", moved.ToLower());

            connector.UnMoveDocument(comObj);

            moved = connector.lookupFieldById(entity.Id, "Проведен");

            Assert.AreEqual("false", moved.ToLower());

            connector.Delete(entity);
        }

        [TestMethod]
        public void TestSaveEntity()
        {
            var entity = new Entity("1С", "Справочник.Тест");
            string code = (new Random().Next(100000) + 1000).ToString();

            entity["Наименование"] = "Hello World!";
            entity["Код"] = code;

            object comObject = connector.ConvertFrom(entity);
            entity.Id = connector.SaveEntity(comObject);

            var id = connector.lookupDictionaryByField("Справочник.Тест", "Код", code);

            Assert.AreEqual(entity.Id, id);

            connector.Delete(entity);
        }

        [TestMethod]
        public void getObjects()
        {
            var filter = new Filter(
                    new List<CAttribute>() { 
                        new CAttribute("Код"),
                        new CAttribute("Наименование"),
                        new CAttribute("ТестСтрока"),
                        new CAttribute("ТестЧисло")
                        },
                    new List<CCondition>() { 
                        new CCondition("Наименование", "=", "123")                          
                    }
                );

            var entities = connector.getObjects(filter);

            Assert.AreEqual(1, entities.Count);
            Assert.AreEqual("1", entities[0]["Код"]);
            Assert.AreEqual("123", entities[0]["Наименование"]);
            Assert.AreEqual("456", entities[0]["ТестСтрока"]);
            Assert.AreEqual("10.20", entities[0]["ТестЧисло"]);
        }

        [TestMethod]
        public void TestCheckHealth()
        {
            var health = connector.CheckHealth();

            Assert.IsTrue(health.Status);
        }

        [TestMethod]
        public void Testclose()
        {
            connector.close();
        }

        public OSConnector77Tests()
        {
            connector = new OSConnector77(new Dictionary<string, string> 
            { 
                { "path", @"D:\Проекты\1CBase\" }, 
                { "user", "" }, 
                { "password", "" }
            });
        }

    }
}
